var moduleMenuModule = angular.module('moduleMenuModule', ['paginationService', 'anajaxService','agularValidate','innerPaginationService']);

moduleMenuModule.controller('moduleMenuController', function($scope, $http,pagination,anajax,innerPagination) {
	
	
	$scope.resultList={};//结果列表
	$scope.queryForm={};
	$scope.moduleMenuForm={};
	$scope.moduleMenuDtoForm={};
	$scope.isLeafOps={1:"是",0:"否"};
	
	/**
	 * 查询
	 */
	var query = function(params) {
		/*var url = commonutil.actionPath + '/module/menu/list_json?id='+id+'&isMeun='+isMeun;*/
		var url = commonutil.actionPath + '/module/menu/list_json';
		anajax.doajax(url, params, function(data) {
			$scope.resultList = data.resultList;
			pagination.setData(data.pagination);
			$scope.pagination = data.pagination; 
		});
	};
	
	
	/**
	 * 跳转到指定页
	 */
	$scope.goPage = function(selNum) {
		
		pagination.goPage(selNum);
		query($scope.pagination);
	};
	
	/**
	 * 上一页
	 */
	$scope.lastPage = function() {
		pagination.lastPage();
		query($scope.pagination);
	};
	
	/**
	 * 下一页
	 */
	$scope.nextPage = function() {
		pagination.nextPage();
		query($scope.pagination);
	};
	
	/**
	 * 点击查询
	 */
	$scope.doquery = function() {
		
		query($scope.queryForm);
	};
	
	/**
	
	
	/**初始化新增*/
	$scope.goAdd = function(index) {
		$scope.moduleMenuForm = {};
		$scope.stations = [];
		$scope.rightPart = 'add';
		$scope.moduleMenuForm = {'invoice':'是'};
		removeTips($('#edit-data-warp'));
		pageutil.showRightBarAn('新增菜单');
	};
	
	/**初始化编辑*/
	$scope.goModify  = function(index) {
		$scope.rightPart = 'edit';
		var assets=$scope.resultList[index];
		angular.copy(assets,$scope.moduleMenuForm);
		
		$scope.moduleMenuForm.i18n_Name='{"en_US": '+assets.i18nNameUS+', "zh_CN": '+assets.i18nNameCN+'}';
		$scope.moduleMenuForm.createTime=null;
		$scope.moduleMenuForm.lastUpdateTime=null;
		removeTips($('#edit-data-warp'));
		pageutil.showRightBarAn('编辑菜单');
	};
	
	
	
	
	$scope.doCancel = function(){
		$scope.moduleMenuForm = {};
		pageutil.hideRightBar();
	}
	
	
	/**
	 * 执行添加
	 */
	$scope.doAdd = function() {
		var res = Validator.validate($("#edit-data-warp"));
		if(!res){
			return;
		}
		if($scope.rightPart=='add'){
			$scope.add($scope.moduleMenuForm);
		}else{
			$scope.modify($scope.moduleMenuForm);
		}
		
	};
	
	/**
	 * 执行添加
	 */
	$scope.add = function(postData) {
		var parentId = $("#parentId").val()
		var moduleInfoId = $("#moduleInfoId").val();
		var url = commonutil.actionPath + '/module/menu/add?parentId='+parentId+'&moduleInfoId='+moduleInfoId;
		anajax.doajax(url, postData, function(data) {
			if (data.resultValue ) {
				pageutil.showTip('添加成功');
				pageutil.hideRightBar();
				$scope.getModulenfo(moduleInfoId);
				$scope.queryForm.parentId = parentId;
				/*$scope.doquery();*/
				query($scope.queryForm);
				
			
			} else {
				pageutil.showTip(data.message);
			}
		});
	};
	
	/**
	 * 执行修改
	 */
	$scope.modify = function(postData) {
		var parentId = $("#parentId").val()
		var moduleInfoId = $("#moduleInfoId").val();
		var url = commonutil.actionPath + '/module/menu/update';
		anajax.doajax(url, postData, function(data) {
			if (data.resultValue ) {
				pageutil.showTip('修改成功');
				pageutil.hideRightBar();
				$scope.getModulenfo(moduleInfoId);
				$scope.queryForm.parentId = parentId;
				query($scope.queryForm);
				$scope.doquery();
			} else {
				pageutil.showTip(data.message);
			}
		});
	};
	
	$scope.delBean ={};
	
	/**
	 * 弹出确认删除窗口
	 */
	$scope.showDelWin = function($index){
		var assetBean = $scope.resultList[$index];
		
		$scope.delBean= assetBean;
		$scope.delBean.createTime=null;
		$scope.delBean.lastUpdateTime=null;
		$("#confirm_del_asset_div").modal('show');
	};
	
	$scope.doDelAsset = function() {
		var parentId = $("#parentId").val()
		var moduleInfoId = $("#moduleInfoId").val();
	    console.log($scope.delBean);
		var url = commonutil.actionPath + '/module/menu/delete';
		anajax.doajax(url,$scope.delBean, function(data) {
			if (data.resultValue ) {
				pageutil.showTip('删除成功');
				$scope.getModulenfo(moduleInfoId);
				$scope.queryForm.parentId = parentId;
				query($scope.queryForm);
				$scope.doquery();
			} else {
				pageutil.showTip(data.message);
			}
		});
	};
	
	
	/**
	 * 选择模块
	 */
	$scope.getModulenfo  = function(id){
	$scope.queryForm.parentId = -1;
	query($scope.queryForm);
	/*var id=$scope.selected;*/
	if(id == null){
		id=$scope.selected;
	}
	
	var parentId = $("#parentId").val();
	var moduleMenuId = $("#moduleMenuId").val();
	if(id!=parentId){
		var parentId = $("#parentId").val(0);
	}
	$("#moduleInfoId").val(id);
	if(id==-1){
		return false;
	}
	$("#aa").innerHTML="";;
	/*$("#aa").hide();*/
	var url = commonutil.actionPath + '/module/menu/listModuleInfos?id='+id;
/*	$("#grid-table").clearGridData(false);*/
	//模拟ajax请求
	 $.ajax({
		  type : "GET",
			url : url,
			dataType : 'json',
			async : false,
			success : function(data) {
				var treeNodes =data;
				var setting={	
						data:{
							key:{
								name:"menuName",
								title:"menuName"
							},
							simpleData:{
								enable:true,
								idKey:"moduleMenuId",
								pIdKey:"parentId"
							},
							keep: {
								parent: true
							}
						},
						callback: {
							onClick: zTreeOnClick
						},
						async: {
							enable: true
						}
					}
				 $.fn.zTree.init($("#tree"), setting, treeNodes);
				 var treeObj = $.fn.zTree.getZTreeObj("tree");
				 treeObj.expandAll(true);
			}
	  });
}

	
	//树形菜单点击事件
	function zTreeOnClick(event, treeId, treeNode) {
		$("#moduleMenuId").val(treeNode.moduleMenuId);
		$("#parentId").val(treeNode.moduleMenuId);
		var moduleMenuId=treeNode.moduleMenuId;
		var parentId = $("#parentId").val();
		$scope.queryForm.parentId = treeNode.moduleMenuId;
		$scope.queryForm.moduleInfoId = treeNode.moduleInfoFlag;
		var isMeun = true;
		query($scope.queryForm);
		$("#aa").show();
		var treeObj = $.fn.zTree.getZTreeObj("tree");
		var sNodes = treeObj.getSelectedNodes();
		if (sNodes.length > 0) {
			var level = sNodes[0].level;
			if(level==0||sNodes[0]!=null&&sNodes[0].children!=null&&sNodes[0].children.length!=null&&sNodes[0].children.length>0){
				$("#isLastMenu").val("true");
			}else{
				$("#isLastMenu").val("false");
			}
		}

		
	}

	
});